package vnc.ui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.Border;

public class IP4bit extends JPanel{
	
	public String[] names;
	public Object[] values;

	Point LocationBox = new Point(0,0);
	Dimension PaneSize = new Dimension(600,150); 
	JLabel jl= new JLabel();

	//JTextArea jl = new JTextArea();
	
	JButton Version;
	JButton HLen;
	JButton ServiceType;
	JButton TLen;
	JButton Identification;
	JButton Flags;
	JButton FragOffset;
	JButton TTL;
	JButton Protocol;
	JButton HChecksum;
	JButton SourceIP;
	JButton DestinationIP;

	public IP4bit(){
		this.setLayout(null);
		this.setBackground(Color.white);
	
		Version = new JButton("Version");
		HLen = new JButton("H Len");
		ServiceType = new JButton("Service Type");
		TLen = new JButton("Total Length");
		Identification = new JButton("Identification");
		Flags = new JButton("Flags");
		FragOffset = new JButton("Frag Offset");
		TTL = new JButton("Time to live");
		Protocol = new JButton("Protocol");
		HChecksum = new JButton("Header Checksum");
		SourceIP = new JButton("Source IP");
		DestinationIP = new JButton("Destination IP");
		
		this.removeBoxBorder(Version);
		this.removeBoxBorder(HLen);
		this.removeBoxBorder(ServiceType);
		this.removeBoxBorder(TLen);
		this.removeBoxBorder(Identification);
		this.removeBoxBorder(Flags);
		this.removeBoxBorder(FragOffset);
		this.removeBoxBorder(TTL);
		this.removeBoxBorder(Protocol);
		this.removeBoxBorder(HChecksum);
		this.removeBoxBorder(SourceIP);
		this.removeBoxBorder(DestinationIP);
	
		Version.setBackground(new Color(0xff,0x45,0x00));
		 HLen.setBackground(new Color(0xff,0x45,0x00));
		 ServiceType.setBackground(new Color(0xff,0x45,0x00));
		 TLen.setBackground(new Color(0xff,0x45,0x00));
		
		 Identification.setBackground(new Color(0xff,0x63,0x47));
		 Flags.setBackground(new Color(0xff,0x63,0x47));
		 FragOffset.setBackground(new Color(0xff,0x63,0x47));
		 
		 TTL.setBackground(new Color(0xff,0x7f,0x50));
		 Protocol.setBackground(new Color(0xff,0x7f,0x50));
		 HChecksum.setBackground(new Color(0xff,0x7f,0x50));
		 
		 SourceIP.setBackground(new Color(0xff,0x8c,0x00));
		 
		 DestinationIP.setBackground(new Color(0xff,0xa5,0x00));
	
		this.add(Version);
		this.add(HLen);
		this.add(ServiceType);
		this.add(TLen);
		this.add(Identification);
		this.add(Flags);
		this.add(FragOffset);
		this.add(TTL);
		this.add(Protocol);
		this.add(HChecksum);
		this.add(SourceIP);
		this.add(DestinationIP);
		
		jl.setBounds(0, 160, 600, 120);
		//jl.setWrapStyleWord(true);
		this.add(jl);
	}
	
	public static void removeBoxBorder(JButton box){
		box.setBorderPainted(true);
		box.setContentAreaFilled(true);
		box.setDefaultCapable(false);
		box.setFocusPainted(false);
	}

	public void getInfos(final String[] names,final Object[] values,final int len){
		
		
		Font f = new Font("",Font.BOLD,13);
		jl.setFont(f);
		
		this.names = names;
		this.values = values;
		
		Version.setBounds(LocationBox.x, LocationBox.y,PaneSize.width/8, PaneSize.height/5);
		Version.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				jl.setText(names[0]+" : "+values[0]);
			}
		});
		HLen.setBounds(Version.getX()+Version.getWidth(), LocationBox.y,PaneSize.width/8, PaneSize.height/5);
		HLen.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				jl.setText("Header Length : "+len);
			}
		});
		ServiceType.setBounds(HLen.getX()+HLen.getWidth(), LocationBox.y,PaneSize.width/4, PaneSize.height/5);
		ServiceType.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				StringBuffer sb = new StringBuffer();
				for(int i=1;i<=3;i++){
					sb.append(names[i]+" : "+values[i]+"<br></br>");
				}
				jl.setText("<html>"+sb.toString()+"</html>");
	
			}
		});
		TLen.setBounds(ServiceType.getX()+ServiceType.getWidth(), LocationBox.y,PaneSize.width/2, PaneSize.height/5);
		TLen.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				jl.setText(names[4]+" : "+values[4]+'\n');
				
			}
		});
		
		Identification.setBounds(LocationBox.x, LocationBox.y + Version.getHeight(),PaneSize.width/2, PaneSize.height/5);
		Identification.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				jl.setText(names[5]+" : "+values[5]);
			}
		});
		Flags.setBounds(LocationBox.x + Identification.getWidth() , LocationBox.y + Version.getHeight(),PaneSize.width/4, PaneSize.height/5);
		Flags.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				StringBuffer sb = new StringBuffer();
				for(int i=6;i<=7;i++){
					sb.append(names[i]+" : "+values[i]+"<br></br>");
				}
				jl.setText("<html>"+sb.toString()+"</html>");
	
			}
		});
		FragOffset.setBounds(Flags.getX() + Flags.getWidth(), LocationBox.y + Version.getHeight(),PaneSize.width/4, PaneSize.height/5);
		FragOffset.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				jl.setText(names[8]+" : "+values[8]);
			}
		});
		
		TTL.setBounds(LocationBox.x, LocationBox.y +FragOffset.getHeight() + Version.getHeight(),PaneSize.width/4, PaneSize.height/5);
		TTL.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				jl.setText(names[9]+" : "+values[9]);
			}
		});
		Protocol.setBounds(LocationBox.x + TTL.getWidth(), LocationBox.y +Identification.getHeight() + Version.getHeight(),PaneSize.width/4, PaneSize.height/5);
		Protocol.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				jl.setText(names[10]+" : "+values[10]);
			}
			
		});
		HChecksum.setBounds(Protocol.getX()+Protocol.getWidth(), LocationBox.y +Identification.getHeight() + Version.getHeight(),PaneSize.width/2, PaneSize.height/5);
		HChecksum.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				jl.setText("nothing");
			}
		});
		
		
		SourceIP.setBounds(LocationBox.x, LocationBox.y+TTL.getHeight() +Identification.getHeight() + Version.getHeight(),PaneSize.width, PaneSize.height/5);
		SourceIP.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				jl.setText(names[11]+" : "+values[11]);
			}
		});
		DestinationIP.setBounds(LocationBox.x, LocationBox.y +SourceIP.getHeight()+ TTL.getHeight() +Identification.getHeight() + Version.getHeight(),PaneSize.width, PaneSize.height/5);
		DestinationIP.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				jl.setText(names[12]+" : "+values[12]);
			}
		});
	}

	
	public void getInfos(String[] names, Object[] values) {
		// TODO Auto-generated method stub
		
	}


	public String isProtocol() {
		// TODO Auto-generated method stub
		return "IPv4";
	}

	
}
